package cn.org.seventh.tools.plugin;

import org.generama.FreeMarkerTemplateEngine;
import org.generama.JellyTemplateEngine;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.WriterMapper;
import org.generama.defaults.QDoxPlugin;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

public class MethodUrlMappingPlugin extends QDoxPlugin {

	private String filename = "mapping.properties";

	public MethodUrlMappingPlugin(JellyTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(false);
		new TagLibrary(metadataProvider);
	}

	public MethodUrlMappingPlugin(FreeMarkerTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(false);
		new TagLibrary(metadataProvider);
	}

	public String getName(Object metadata) throws Exception {
		String str = getClassNamedParameter(metadata, "name", null);
		if (str == null || "".equals(str)) {
			throw new Exception("value must not be  empty! ");
		}
		String[] strs = str.split("\\**/*$");
		if (strs.length > 0) {
			str = strs[0];
		}
		if (!str.startsWith("/")) {
			str = "/" + str;
		}
		if (str.endsWith("/")) {
			str = str.substring(0, str.length() - 1);
		}
		return str;
	}

	public String getUrl(Object metadata) throws Exception {
		String str = getMethodNamedParameter(metadata, "url", null);
		if (str == null) {
			return null;
		}
		if ("".equals(str)) {
			throw new Exception("value must not be  empty! ");
		}
		if (!str.startsWith("/")) {
			str = "/" + str;
		}
		if (str.endsWith("/")) {
			str = str.substring(0, str.length() - 1);
		}
		return str;
	}

	private String getMethodNamedParameter(Object metadata, String name,
			String value) {
		JavaMethod javaMethod = (JavaMethod) metadata;
		String str = javaMethod.getNamedParameter(MethodUrlTagImpl.NAME, name);
		return null == str ? value : str.trim();
	}

	private String getClassNamedParameter(Object metadata, String name,
			String value) {
		JavaClass javaClass = (JavaClass) metadata;
		String str = javaClass.getNamedParameter(SpringWebTagImpl.NAME, name);
		return null == str ? value : str.trim();
	}

	public boolean isMultiActionController(Object metadata) throws Exception {
		JavaClass javaClass = (JavaClass) metadata;
		String str = javaClass.getNamedParameter(SpringWebTagImpl.NAME,
				"simple");
		str = null == str ? "true" : str;
		if ("true".equalsIgnoreCase(str)) {
			return false;
		} else {
			return true;
		}

		// try {
		// boolean b = Class
		// .forName(
		// "org.springframework.web.servlet.mvc.multiaction.MultiActionController")
		// .isAssignableFrom(
		// Class.forName(javaClass.getFullyQualifiedName()));
		// return b;
		// } catch (Exception e) {
		// e.printStackTrace();
		// throw new Exception(
		// "it is not a sub class of MultiActionController!");
		// }
	}

	public boolean shouldGenerate(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		return javaClass.getTagByName(SpringWebTagImpl.NAME) == null ? false
				: true;
	}

	public String getDestinationFilename(Object metadata) {
		return filename;
	}

	public String getDestinationPackage(Object metadata) {
		return ".";
	}

	public String getFilename() {
		return this.filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}
}